<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\model\BusinessVideo;
use Qiniu\Auth;
use think\App;
use app\common\builder\TableBuilder;
use app\common\builder\TableFront;
use think\facade\View;
use think\response\Json;
use app\common\model\Product;

class Video extends Base
{

    //主键
    protected $pk = 'id';

    //主模型
    protected $model = '\app\common\model\BusinessVideo';

    protected $hasOne = ['product', 'business'];

    //额外查询条件
    protected $extraSearch = ['business_id'];

    public function apply(): string
    {
        $extra_map = ['status'=>1];
        return $this->setTable($extra_map);
    }

    public function index(): string
    {
        $extra_map = ['status'=>2];
        return $this->setTable($extra_map);
    }

    //自动构建table,按需修改
    public function setTable($extra_map=[]): string
    {
        $tableFront = TableFront::getInstance()
            ->addRow(['type' => 'checkbox', 'fixed' => 'left', 'title' => '全选'])
            ->addRow(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addRow(['field' => 'introduce', 'title' => '介绍']);
            if($extra_map['status'] == 1){
                $tableFront->addRow(['field' => 'total_hit', 'title' => '点赞量', 'width' => 110])
                    ->addRow(['field' => 'total_comment', 'title' => '评论量', 'width' => 110]);
            }

            $tableFront->addRow(['field' => 'business', 'title' => '所属商户', 'width' => 110, 'templet' => '<div>{{d.business?d.business.title:"--"}}</div>'])
            ->addRow(['field' => 'product', 'title' => '关联产品', 'width' => 110, 'templet' => '<div>{{d.product?d.product.title:"--"}}</div>'])
            ->addRow(['field' => 'show', 'title' => '是否显示', 'templet' => '#show', 'width' => 120, 'unresize' => true])
            ->addRow(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作', 'width' => 150]);

            $BusinessApply = [];
            if($extra_map['status'] == 1){
                $BusinessApply = ['BusinessApply'];
            }

            $right = $extra_map['status'] == 1 ? '审核' : '查看';

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->addRightButtons(['edit' => ['title' => $right, 'width' => 1920, 'height' => 1080]])
            ->setSearch($this->setSearch())
            ->addExtraMap($extra_map)
            ->addExtraHtml($BusinessApply)
            ->addSwitchTool([['name' => 'show', 'pk' => 'id', 'text' => '显示|下架']])
            ->fetch();
    }

    //自动构建Search，按需修改
    public function setSearch()
    {
        return TableFront::getInstance()
            ->setSearch(['introduce', '介绍', 'input', '', '', '', 'like']);

    }

    public function edit($name = '', $id = '', $tpl = 'video/edit', $info = '')
    {
        $config = config('qiniu');
        // 构建鉴权对象
        $auth = new Auth($config['accessKey'], $config['secretKey']);
        $token = $auth->uploadToken($config['bucket']);

        $info = $this->model::find($id);
        $info->video_link = $info->link;
        $info->video_link_url = $config['domain'].$info->link;

        View::assign('token', $token);
        View::assign('domain', $config['domain']);
        View::assign('product', Product::where('id', $info->product_id)->value('title'));
        return parent::edit($name, $id, $tpl, $info);
    }

    public function audit(): Json
    {
        $result = BusinessVideo::audit($this->request->param(), $this->admin);
        return json($result);
    }
}
